//
//  YWNewCommentCell.m
//  maintain
//
//  Created by 郑想 on 16/7/22.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWNewCommentCell.h"
#import "YWCommonTool.h"
#import "YWHandler.h"
#import "YWReply.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "UIImageView+WebCache.h"
#import "UILabel+AttributedText.h"

@interface YWNewCommentCell()

@property (nonatomic, strong) UIImageView *commentView;
/** 头像 */
@property (nonatomic, strong) UIImageView *headPortraitView;
/** 名字 */
@property (nonatomic, strong) UILabel *nameLabel;
/** 时间 */
@property (nonatomic, strong) UILabel *timeLable;
/** 评论内容 */
@property (nonatomic, strong) UILabel *commentLabel;

@end



@implementation YWNewCommentCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    YWNewCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:YWNewCommentCellIdetifer];
    if (cell == nil) {
        cell = [[YWNewCommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:YWNewCommentCellIdetifer];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.separatorInset = UIEdgeInsetsMake(0, SCREEN_WIDTH, 0, 0);
    }
    return self;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    self.separatorInset = UIEdgeInsetsMake(0, SCREEN_WIDTH, 0, 0);
}

-(void)setReply:(YWReply *)reply{
    _reply = reply;
    
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:reply.replier.headIcon];
    //    头像
    [_headPortraitView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    //    名字
    _nameLabel.text = reply.replier.userName;
    //    时间
    _timeLable.text = [YWCommonTool recordDateByFormatTimeString:reply.replyTime/1000 showYear:YES];
    //    内容

    NSString *atUserName = reply.atUser.userName.length>0?[NSString stringWithFormat:@"@%@",reply.atUser.userName]:@"";
    NSString * content = atUserName.length?[NSString stringWithFormat:@" %@",reply.content.length>0?reply.content:@""] : (reply.content.length>0?reply.content:@"");
    [_commentLabel labelText:atUserName withAttributedText:content andAttributedColor:[UIColor blackColor]];
}

- (void)setUpSubviews{
    UIView *supView = self.contentView;
    [self.commentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(23, 23));
        make.left.and.top.mas_equalTo(supView).offset(8);
    }];
    
    [self.headPortraitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(34, 34));
        make.top.mas_equalTo(supView).offset(6);
        make.left.mas_equalTo(_commentView.mas_right).offset(5);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_headPortraitView.mas_centerY).offset(-2);
        make.left.mas_equalTo(_headPortraitView.mas_right).offset(6);
    }];
    
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_nameLabel.mas_left);
        make.right.mas_equalTo(supView.mas_right).offset(-8);
        make.top.mas_equalTo(_nameLabel.mas_bottom).offset(4);
        make.bottom.mas_equalTo(supView).offset(-6);
    }];
    
    [self.timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(supView).offset(8);
        make.right.mas_equalTo(supView).offset(-8);
    }];
}
- (UIImageView *)commentView{
    if (!_commentView) {
        UIImageView *imageView = [UIImageView new];
        imageView.image = [UIImage imageNamed:@"remind_replyIcon"];
        _commentView = imageView;
        _commentView.tag = 101;
        [self.contentView addSubview:_commentView];
    }
    return _commentView;
}

- (UIImageView *)headPortraitView{
    if (!_headPortraitView) {
        UIImageView *imageView = [UIImageView new];
        imageView.backgroundColor = [UIColor customTextGrayColor];
        imageView.clipsToBounds = YES;
        imageView.layer.cornerRadius = 34/2;
        _headPortraitView = imageView;
        [self.contentView addSubview:_headPortraitView];
    }
    return _headPortraitView;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 1;
        label.font = kContentFont;
        label.textColor = [UIColor customBlueColor];
        _nameLabel = label;
        [self.contentView addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UILabel *)commentLabel{
    if (!_commentLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 0;
        label.font = kContentFont;
        label.textColor = [UIColor customBlueColor];
        _commentLabel = label;
        [self.contentView addSubview:_commentLabel];
    }
    return _commentLabel;
}

- (UILabel *)timeLable{
    if (!_timeLable) {
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 1;
        label.font = KSmallContentFont;
        label.textColor = [UIColor customTextGrayColor];
        _timeLable = label;
        [self.contentView addSubview:_timeLable];
    }
    return _timeLable;
}

@end
